package com.tools.controller.workflow;

import com.tools.common.result.Result;
import com.tools.pojo.workflow.bo.FlowDefinitionRequest;
import com.tools.pojo.workflow.po.FlowDefinition;
import com.tools.service.workflow.FlowDefinitionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 流程定义控制器
 */
@Slf4j
@RestController
@RequestMapping("/workflow/definition")
@RequiredArgsConstructor
public class FlowDefinitionController {

    private final FlowDefinitionService flowDefinitionService;

    /**
     * 根据流程代码查询流程定义
     */
    @GetMapping("/getByCode/{flowCode}")
    public Result<FlowDefinition> getByCode(@PathVariable String flowCode) {
        return Result.success(flowDefinitionService.getByCode(flowCode));
    }

    /**
     * 根据流程代码和版本查询流程定义
     */
    @GetMapping("/getByCodeAndVersion")
    public Result<FlowDefinition> getByCodeAndVersion(
            @RequestParam String flowCode, 
            @RequestParam Integer version) {
        return Result.success(flowDefinitionService.getByCodeAndVersion(flowCode, version));
    }
    /**
     * 查询所有活跃的流程定义
     */
    @GetMapping("/active")
    public Result<List<FlowDefinition>> getActiveFlows() {
        return Result.success(flowDefinitionService.getActiveFlows());
    }
    /**
     * 解析流程配置JSON字符串
     */
    @PostMapping("/parseConfig")
    public Result<List<Map<String, Object>>> parseFlowConfig(@RequestBody Map<String, String> request) {
        String configJson = request.get("configJson");
        return Result.success(flowDefinitionService.parseFlowConfig(configJson));
    }
    /**
     * 创建新的流程定义版本
     */
    @PostMapping("/createNewVersion")
    public Result<FlowDefinition> createNewVersion(
            @RequestBody FlowDefinitionRequest request) {
        try {
            FlowDefinition flowDefinition = new FlowDefinition();
            flowDefinition.setFlowCode(request.getFlowCode());
            flowDefinition.setFlowName(request.getFlowName());
            flowDefinition.setDescription(request.getDescription());
            flowDefinition.setConfigJson(request.getConfigJson());
            
            FlowDefinition created = flowDefinitionService.createNewVersion(
                    flowDefinition, request.getNodes(), request.getTransitions());
            return Result.success(created);
        } catch (Exception e) {
            log.error("创建新版本流程定义失败", e);
            return Result.fail("创建失败：" + e.getMessage());
        }
    }
    /**
     * 发布流程定义
     */
    @PostMapping("/publish/{definitionId}")
    public Result<Boolean> publishDefinition(@PathVariable Long definitionId) {
        try {
            boolean result = flowDefinitionService.publishDefinition(definitionId);
            return result ? Result.success(true) : Result.fail("发布失败：流程定义不存在");
        } catch (Exception e) {
            log.error("发布流程定义失败", e);
            return Result.fail("发布失败：" + e.getMessage());
        }
    }
    /**
     * 停用流程定义
     */
    @PostMapping("/deactivate/{definitionId}")
    public Result<Boolean> deactivateDefinition(@PathVariable Long definitionId) {
        try {
            boolean result = flowDefinitionService.deactivateDefinition(definitionId);
            return result ? Result.success(true) : Result.fail("停用失败：流程定义不存在");
        } catch (Exception e) {
            log.error("停用流程定义失败", e);
            return Result.fail("停用失败：" + e.getMessage());
        }
    }
    /**
     * 查询流程定义的版本历史
     */
    @GetMapping("/versionHistory/{flowCode}")
    public Result<List<FlowDefinition>> getVersionHistory(@PathVariable String flowCode) {
        return Result.success(flowDefinitionService.getVersionHistory(flowCode));
    }
    /**
     * 同步流程定义到缓存
     */
    @PostMapping("/syncToCache/{definitionId}")
    public Result<Void> syncToCache(@PathVariable Long definitionId) {
        flowDefinitionService.syncDefinitionToCache(definitionId);
        return Result.success();
    }

}
